<?php 

define('SEPARATOR', "/");
define('DEFPATH', "/mnt/sdb2/Public/www/htdocs");
define('DEFFOLDER', 'media');

function listFolder($folder, $dir){ 

    //headerGeral($dir);
    //$url="http://".$_SERVER['HTTP_HOST']; //.$_SERVER['REQUEST_URI'];
    //$curDir = dirname(__FILE__).SEPARATOR.$folder.SEPARATOR;

    $curDir = DEFPATH.SEPARATOR.DEFFOLDER.SEPARATOR.$folder.SEPARATOR;
    if ($folder=='FM') $curDir = DEFPATH.SEPARATOR.DEFFOLDER.SEPARATOR;

    if(!is_dir($curDir)) {
		echo '<div class="message message-error">Dir: '.$curDir.$dir.' not exist</div>'; 
		return;
    }
	
    // Left side
    echo '<div class="units-row-end">';
    echo '<nav class="nav-tabs nav-tabs-v unit-20">';
    echo '    <ul>';

    $ffs = scandir($curDir); 
    foreach($ffs as $ff){ 
		if($ff != '.' && $ff != '..'){ 
			if(is_dir($curDir.SEPARATOR.$ff)){ 
				if ($dir!=$ff)
					echo '<li><a href="index.php?target='.$ff.'">'.$ff.'</a></li>';
				else
					echo '<li><span><b>'.$ff.'</b></span></li>';
			}
		}
    } 
    echo '    </ul>';
    echo '</nav>';

    if ($folder=='FM') { listFM($dir); return; }
	
    // Folders
    $noFolders=0;
    echo '<section class="unit-80">';
    if(!is_dir($curDir.$dir)) {
		echo '<div class="message message-error">Dir: '.$curDir.$dir.' not exist</div>'; 
    } else {
		echo '<nav class="nav-g"><ul>';
		$ffs = scandir($curDir.$dir); 
		foreach($ffs as $ff){ 
			if($ff != '.' && $ff != '..')
			if(is_dir($curDir.$dir.SEPARATOR.$ff)){ 
				echo '<li><a href="index.php?target='.($dir!=''?$dir.'/':'').$ff.'"><figure><img src="';
				if (file_exists($curDir.$dir.SEPARATOR.$ff.SEPARATOR.'folder.jpg'))
					echo SEPARATOR.DEFFOLDER.SEPARATOR.$folder.'/'.($dir!=''?$dir.'/':'').$ff.'/';
				echo 'folder.jpg" style="width:100px;height:150px" alt="'.$ff.'"></figure></a></li>';
				$noFolders++;
			}
		}
		echo '</ul></nav>';
		if ($noFolders>0) echo 'Folders: '.$noFolders;
    }
    echo '</section>';
	
    // Files
    echo '<section class="unit-80">';
    if(!is_dir($curDir.$dir)) {
	echo '<div class="message message-error">Dir: '.$curDir.$dir.' not exist</div>'; 
    } else {
		echo '<div class="units-row">';
		if ($noFolders==0) {
			echo '<div class="unit-20">';
			echo '<a href="index.php?target='.$dir.'/'.$ff.'"><figure><img src="';
			if (file_exists($curDir.$dir.SEPARATOR.'folder.jpg')) {
				echo '/'.DEFFOLDER.'/'.$folder.'/'.($dir!=''?$dir.'/':'').'folder.jpg">';
				echo '<figcaption>'.$dir.'</figcaption></figure></a></div>'; 
			} else
				echo 'folder.jpg" alt="'.$ff.'"></figure></a>';
		}

		echo '<div class="unit-80"><nav class="nav-v"><ul>';
		$ffs = scandir($curDir.$dir); 
		foreach($ffs as $ff){ 
			if(!is_dir($curDir.$dir.SEPARATOR.$ff)){
				if (substr($ff,strlen($ff)-3)=='mp4' || substr($ff,strlen($ff)-3)=='avi' || substr($ff,strlen($ff)-3)=='mkv' || substr($ff,strlen($ff)-4)=='rmvb') {
					echo '<li>'.substr($ff,0,strlen($ff)-4).'</li>';
					if (file_exists($curDir.$dir.SEPARATOR.substr($ff,0,strlen($ff)-3).'torrent'))
						echo '<li><a href="/'.DEFFOLDER.'/'.$folder.'/'.($dir!=''?$dir.'/':'').substr($ff,0,strlen($ff)-3).'torrent">Torrent</a></li>';
					else
						echo '<li>No download</li>';
					if (file_exists($curDir.$dir.SEPARATOR.substr($ff,0,strlen($ff)-3).'srt'))
						echo '<li><a href="/'.DEFFOLDER.'/'.$folder.'/'.($dir!=''?$dir.'/':'').substr($ff,0,strlen($ff)-3).'srt">Subtitle</a></li>';
					else
						echo '<li>No subtitle</li>';
					if (file_exists($curDir.$dir.SEPARATOR.substr($ff,0,strlen($ff)-3).'imdb')) {
						include $curDir.$dir.SEPARATOR.substr($ff,0,strlen($ff)-3).'imdb';
						//echo '<a href="'.$dir.'/'.substr($ff,0,strlen($ff)-3).'imdb">Info</a>&nbsp;&nbsp;&nbsp;';
					} else
						echo '<li>No IMDB info</li>';
					echo '<li><a href="player.php?file=/'.DEFFOLDER.'/'.$folder.'/'.$dir.'/'.$ff.'&subtitle=/'.DEFFOLDER.'/'.$folder.'/'.$dir.'/'.substr($ff,0,strlen($ff)-3).'srt">Ver</a></li>';
					echo '<li>&nbsp;</li>';
				} else 
					if (substr($ff,strlen($ff)-7)!='torrent' && substr($ff,strlen($ff)-3)!='srt' && substr($ff,strlen($ff)-4)!='imdb' && substr($ff,strlen($ff)-3)!='jpg' && substr($ff,strlen($ff)-3)!='tbn')
						echo '<li><a href="/'.DEFFOLDER.'/'.$folder.'/'.$dir.'/'.$ff.'">'.$ff.'</a></li>';

			}
		}
		echo '</ul></nav></div></div>';
	}
    echo '</section>';
} 

function listFM($dir) { 

	$curDir = DEFPATH.SEPARATOR.DEFFOLDER.SEPARATOR;
  	if(!is_dir($curDir.$dir)) {
		echo '<div class="message message-error">Dir: '.$curDir.$dir.' not exist</div>'; 
		return;
	} 

	echo '<section class="unit-80">';

	echo '<table class="width-100 table-hovered">';
	echo '<thead><tr><th class="width-50">Name</th><th class="width-20">Date</th><th class="width-10">Type</th><th class="width-20">Size</th></tr></thead>';
	echo '<tbody>';

	$ffs = scandir($curDir.$dir); 
	foreach($ffs as $ff) { 
		if($ff != '.' && $ff != '..')
			if(is_dir($curDir.$dir.SEPARATOR.$ff)){ 
				echo '<tr><td><a href="index.php?target='.$dir.SEPARATOR.$ff.'">'.$ff.'</a></td>';
				echo '<td>'.date("d/m/Y H:i:s", filemtime($curDir.$dir.SEPARATOR.$ff)).'</td>';
				echo '<td>Folder</td>';
				echo '<td></td>';
			}
	}

	$ffs = scandir($curDir.$dir); 
	foreach($ffs as $ff) { 
		if($ff != '.' && $ff != '..')
			if(!is_dir($curDir.$dir.SEPARATOR.$ff)){ 
				echo '<tr><td><a href="'.SEPARATOR.DEFFOLDER.SEPARATOR.$dir.SEPARATOR.$ff.'">'.$ff.'</a></td>';
				echo '<td>'.date("d/m/Y H:i:s", filemtime($curDir.$dir.SEPARATOR.$ff)).'</td>';
				echo '<td>File</td>';
				echo '<td>'.formatBytes(filesize($curDir.$dir.SEPARATOR.$ff)).'</td>';
			}
	}

	echo '</tbody>';
	echo '</table>';
	echo '</section>';
}

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 

    // Uncomment one of the following alternatives
    // $bytes /= pow(1024, $pow);
     $bytes /= (1 << (10 * $pow)); 

    return round($bytes, $precision) . ' ' . $units[$pow]; 
} 
?>
